
var page_getProperties = function () {
    var data = egret ? egret.MainContext.instance : {};
    // Make a shallow copy with a null prototype, so that sidebar does not
    // expose prototype.
    var props = Object.getOwnPropertyNames(data);
    var copy = { __proto__: null };
    for (var i = 0; i < props.length; ++i)
        copy[props[i]] = data[props[i]];
    return copy;
};
chrome.devtools.panels.elements.createSidebarPane("Egret Properties", function (sidebar) {
    function updateElementProperties() {
        sidebar.setExpression("(" + page_getProperties.toString() + ")()");
    }
    updateElementProperties();
    chrome.devtools.panels.elements.onSelectionChanged.addListener(updateElementProperties);
});

chrome.devtools.panels.create(
    "Egret", // 面板标题
    "icon.png", // 图标（可选）
    "panel/index.html", // 面板内容页面
    function (panel) {
        var connected = false;
        panel.onShown.addListener(function (w) {
            if (!connected)
                chrome.devtools.inspectedWindow.eval(
                    `
                    (function() {
                        try {
                            if (window.egret?.devtool?.start) {
                                return { success: true, exists: true };
                            }
                            return { success: true, exists: false };
                        } catch (e) {
                            return { success: false, error: e.message };
                        }
                    })()
                    `,
                    function (result, exceptionInfo) {
                        if (result.success) chrome.devtools.inspectedWindow.eval('window.egret.devtool.start()');

                    });
            // chrome.devtools.inspectedWindow.eval('(function () {    var t = window.setInterval(function () { var a = egret && egret.devtool && egret.devtool.start&& (window.clearInterval(t) || egret.devtool.start()); console.log("devtoolinit_waiting") }, 100);egret && egret.devtool && egret.devtool.start&&(window.clearInterval(t) || egret.devtool.start());})();');
            backgroundPageConnection.postMessage({
                toDevTool: true,
                toggleMask: true,
                devToolHidden: false
            });
            connected = true;
        });
        panel.onHidden.addListener(function (w) {
            backgroundPageConnection.postMessage({
                toDevTool: true,
                toggleMask: true,
                devToolHidden: true
            });
        });
        var backgroundPageConnection = chrome.runtime.connect({
            name: btoa("for" + String(chrome.devtools.inspectedWindow.tabId))
        });
        backgroundPageConnection.onMessage.addListener(function (message) {
            // Handle responses from the background page, if any
        });
        backgroundPageConnection.postMessage({
            tabId: chrome.devtools.inspectedWindow.tabId
        });
        panel.onSearch.addListener(function (action, query) {
            return false;
        });
    }
);